package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.CustomerData;
import com.ruoyi.factorydata.domain.CustomerLevelData;
import com.ruoyi.factorydata.domain.CustomerTagData;
import com.ruoyi.factorydata.domain.CustomerTypeData;
import com.ruoyi.factorydata.domain.dto.CustomerDataDTO;
import com.ruoyi.factorydata.domain.vo.CustomerDataVo;
import com.ruoyi.factorydata.mapper.CustomerDataMapper;
import com.ruoyi.factorydata.mapper.CustomerLevelDataMapper;
import com.ruoyi.factorydata.mapper.CustomerTagDataMapper;
import com.ruoyi.factorydata.mapper.CustomerTypeDataMapper;
import com.ruoyi.factorydata.service.ICustomerDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
public class CustomerDataServiceImpl extends ServiceImpl<CustomerDataMapper, CustomerData> implements ICustomerDataService {

    @Autowired
    private CustomerDataMapper customerDataMapper;

    @Autowired
    private CustomerLevelDataMapper customerLevelDataMapper;

    @Autowired
    private CustomerTypeDataMapper customerTypeDataMapper;

    @Autowired
    private CustomerTagDataMapper customerTagDataMapper;

    /**
     * 查询客户数据
     *
     * @param id 客户数据主键
     * @return 客户数据
     */
    @Override
    public CustomerData selectCustomerDataById(Long id) {
        return customerDataMapper.selectCustomerDataById(id);
    }


    /**
     * 查询客户数据列表
     *
     * @param customerDataDTO 客户数据
     * @return 客户数据
     */
    @Override
    public List<CustomerDataVo> selectCustomerDataList(CustomerDataDTO customerDataDTO) {
        CustomerData customerData = new CustomerData();
        customerData.setId(customerDataDTO.getId());
        customerData.setCustomerName(customerDataDTO.getCustomerName());
        customerData.setCustomerCode(customerDataDTO.getCustomerCode());
        customerData.setCustomerName(customerDataDTO.getCustomerName());
        customerData.setKeyword(customerDataDTO.getKeyword());
        List<CustomerData> customerDataList = customerDataMapper.selectCustomerDataList(customerData);
        List<CustomerDataVo> customerDataVos = customerDataList.stream().map(item -> {
            CustomerTypeData customerTypeData = customerTypeDataMapper.selectCustomerTypeDataById(item.getCategoryId());
            CustomerLevelData customerLevelData = customerLevelDataMapper.selectCustomerLevelDataById(item.getLevelId());
            CustomerTagData customerTagData = customerTagDataMapper.selectCustomerTagDataById(item.getId());
            return CustomerDataVo.builder()
                    .id(item.getId())
                    .customerCode(item.getCustomerCode())
                    .customerName(item.getCustomerName())
                    .shortName(item.getShortName())
                    .categoryId(item.getCategoryId())
                    .categoryName(customerTypeData != null ? customerTypeData.getTypeName() : "")
                    .levelId(item.getLevelId())
                    .levelName(customerLevelData != null ? customerLevelData.getLevelName() : "")
                    .tagId(item.getTagId())
                    .tagName(customerTagData != null ? customerTagData.getTagName() : "")
                    .defaultWarehouse(item.getDefaultWarehouse())
                    .defaultWarehouseName("")
                    .province(item.getProvince())
                    .city(item.getCity())
                    .region(item.getRegion())
                    .addressDetail(item.getAddressDetail())
                    .postalCode(item.getPostalCode())
                    .customerStatus(item.getCustomerStatus())
                    .contactName(item.getContactName())
                    .contactPhone(item.getContactPhone())
                    .department(item.getDepartment())
                    .position(item.getPosition())
                    .email(item.getEmail())
                    .qq(item.getQq())
                    .invoiceTitle(item.getInvoiceTitle())
                    .taxId(item.getTaxId())
                    .bankName(item.getBankName())
                    .bankBranch(item.getBankBranch())
                    .accountName(item.getAccountName())
                    .bankAccount(item.getBankAccount())
                    .remark(item.getRemark())
                    .createBy(item.getCreateBy())
                    .createTime(item.getCreateTime())
                    .updateTime(item.getUpdateTime())
                    .build();
        }).collect(Collectors.toList());
        Page<CustomerData> queryData=(Page<CustomerData>)customerDataList;
        Page<CustomerDataVo> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page.setTotal(queryData.getTotal());
        page.addAll(customerDataVos);
        return page;
    }

    /**
     * 新增客户数据
     *
     * @param customerDataDTO 客户数据
     * @return 结果
     */
    @Override
    public int insertCustomerData(CustomerDataDTO customerDataDTO) {
        CustomerData customerData = new CustomerData();
        customerData.setCustomerCode(customerDataDTO.getCustomerCode());
        customerData.setCustomerName(customerDataDTO.getCustomerName());
        customerData.setShortName(customerDataDTO.getShortName());
        customerData.setCategoryId(customerDataDTO.getCategoryId());
        customerData.setLevelId(customerDataDTO.getLevelId());
        customerData.setTagId(customerDataDTO.getTagId());
        customerData.setDefaultWarehouse(customerDataDTO.getDefaultWarehouse());
        // 处理省市区
        if (customerDataDTO.getRegion() != null && !customerDataDTO.getRegion().isEmpty()){
            if (customerDataDTO.getRegion().size() == 2){
                customerData.setProvince(customerDataDTO.getRegion().get(0));
                customerData.setCity(customerDataDTO.getRegion().get(1));
            }else {
                customerData.setProvince(customerDataDTO.getRegion().get(0));
                customerData.setCity(customerDataDTO.getRegion().get(1));
                customerData.setRegion(customerDataDTO.getRegion().get(2));
            }
        }
        customerData.setAddressDetail(customerDataDTO.getAddressDetail());
        customerData.setPostalCode(customerDataDTO.getPostalCode());
        customerData.setContactName(customerDataDTO.getContactName());
        customerData.setContactPhone(customerDataDTO.getContactPhone());
        customerData.setDepartment(customerDataDTO.getDepartment());
        customerData.setPosition(customerDataDTO.getPosition());
        customerData.setEmail(customerDataDTO.getEmail());
        customerData.setQq(customerDataDTO.getQq());
        customerData.setInvoiceTitle(customerDataDTO.getInvoiceTitle());
        customerData.setTaxId(customerDataDTO.getTaxId());
        customerData.setBankName(customerDataDTO.getBankName());
        customerData.setBankBranch(customerDataDTO.getBankBranch());
        customerData.setAccountName(customerDataDTO.getAccountName());
        customerData.setBankAccount(customerDataDTO.getBankAccount());
        customerData.setCustomerStatus(customerDataDTO.getCustomerStatus());
        customerData.setRemark(customerDataDTO.getRemark());
        customerData.setCreateBy(customerDataDTO.getCreateBy());
        customerData.setCreateTime(DateUtils.getNowDate());
        return customerDataMapper.insertCustomerData(customerData);
    }

    /**
     * 修改客户数据
     *
     * @param customerDataDTO 客户数据
     * @return 结果
     */
    @Override
    public int updateCustomerData(CustomerDataDTO customerDataDTO) {
        CustomerData customerData = new CustomerData();
        customerData.setId(customerDataDTO.getId());
        customerData.setCustomerCode(customerDataDTO.getCustomerCode());
        customerData.setCustomerName(customerDataDTO.getCustomerName());
        customerData.setShortName(customerDataDTO.getShortName());
        customerData.setCategoryId(customerDataDTO.getCategoryId());
        customerData.setLevelId(customerDataDTO.getLevelId());
        customerData.setTagId(customerDataDTO.getTagId());
        customerData.setDefaultWarehouse(customerDataDTO.getDefaultWarehouse());
        // 处理省市区
        if (customerDataDTO.getRegion() != null && !customerDataDTO.getRegion().isEmpty()){
            if (customerDataDTO.getRegion().size() == 2){
                customerData.setProvince(customerDataDTO.getRegion().get(0));
                customerData.setCity(customerDataDTO.getRegion().get(1));
                customerData.setRegion("");
            }else {
                customerData.setProvince(customerDataDTO.getRegion().get(0));
                customerData.setCity(customerDataDTO.getRegion().get(1));
                customerData.setRegion(customerDataDTO.getRegion().get(2));
            }
        }
        customerData.setAddressDetail(customerDataDTO.getAddressDetail());
        customerData.setPostalCode(customerDataDTO.getPostalCode());
        customerData.setContactName(customerDataDTO.getContactName());
        customerData.setContactPhone(customerDataDTO.getContactPhone());
        customerData.setDepartment(customerDataDTO.getDepartment());
        customerData.setPosition(customerDataDTO.getPosition());
        customerData.setEmail(customerDataDTO.getEmail());
        customerData.setQq(customerDataDTO.getQq());
        customerData.setInvoiceTitle(customerDataDTO.getInvoiceTitle());
        customerData.setTaxId(customerDataDTO.getTaxId());
        customerData.setBankName(customerDataDTO.getBankName());
        customerData.setBankBranch(customerDataDTO.getBankBranch());
        customerData.setAccountName(customerDataDTO.getAccountName());
        customerData.setBankAccount(customerDataDTO.getBankAccount());
        customerData.setCustomerStatus(customerDataDTO.getCustomerStatus());
        customerData.setRemark(customerDataDTO.getRemark());
        customerData.setUpdateBy(customerDataDTO.getUpdateBy());
        customerData.setUpdateBy(customerDataDTO.getUpdateBy());
        customerData.setUpdateTime(DateUtils.getNowDate());
        return customerDataMapper.updateCustomerData(customerData);
    }

    /**
     * 批量删除客户数据
     *
     * @param ids 需要删除的客户数据主键
     * @return 结果
     */
    @Override
    public int deleteCustomerDataByIds(Long[] ids) {
        int i = customerDataMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除客户数据信息
     *
     * @param id 客户数据主键
     * @return 结果
     */
    @Override
    public int deleteCustomerDataById(Long id) {
        return customerDataMapper.deleteCustomerDataById(id);
    }

    @Override
    public String getNameByIdAndCache(Long customerId, HashMap<Long, String> customNameCache) {
        return CacheSupportUtils.getFromCacheOrDB(customerId,customNameCache,this::selectCustomerDataById, CustomerData::getCustomerName,false);
    }

    @Override
    public String getCodeByIdAndCache(Long customerId, HashMap<Long, String> customCodeCache) {
        return CacheSupportUtils.getFromCacheOrDB(customerId,customCodeCache,this::selectCustomerDataById, CustomerData::getCustomerCode,false);

    }
}